package com.gzist.springailab06.controller;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiAudioSpeechModel;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.ai.openai.audio.speech.SpeechPrompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * ai绘图测试接口
 */
@RestController
@RequestMapping("/springAi")
@RequiredArgsConstructor
public class ImageController {

    private static final Logger logger = LoggerFactory.getLogger(ImageController.class);
    private final OpenAiImageModel openAiImageModel;

    /**
     * 调用openai的接口
     * public ImageResponse call(ImagePrompt imagePrompt){}
     * @param msg：我们提问的消息
     * @return
     */
    @GetMapping("/image")
    public String image(String msg){
        logger.info("msg：{}",msg );
        ImagePrompt imagePrompt = new ImagePrompt(msg);
        ImageResponse imageResponse = openAiImageModel.call(imagePrompt);
        String imageUrl = imageResponse.getResult().getOutput().getUrl();
        logger.info("imageUrl:{}",imageUrl);
        return imageResponse.getResult().getOutput().getUrl();
    }

    /**
     * 调用openai的接口
     * public ImageResponse call(ImagePrompt imagePrompt){}
     * @param msg：我们提问的消息
     * @return
     */
    @GetMapping("/image2")
    public List<String> image2(String msg){
        logger.info("msg：{}",msg );
        List<String> urls = new ArrayList<>();
        OpenAiImageOptions imageOptions = OpenAiImageOptions.builder()
                //高清图像
                .withQuality("hd")
                //生成四张
                .withN(4)
                //高度
                .withHeight(1024)
                //宽度
                .withWidth(1024).build();

        ImagePrompt imagePrompt = new ImagePrompt(msg,imageOptions);
        openAiImageModel.call(imagePrompt).getResults().forEach(imageResult -> urls.add(imageResult.getOutput().getUrl()));
        logger.info("imageUrls:{}",urls);
        return urls;
    }
}